<template>
	<view class="content">
		<uni-nav-bar dark :fixed="true" shadow background-color="#0D4289" status-bar left-icon="left" left-text=""
			title="寰宇青岛MOM平台" @clickLeft="back" />
		<image :src="base.request.imageUrl+'/internet/WechatIMG534.jpeg'" style="width: 100%;height:12rem"></image>

		<scroll-view :scroll-y="true">
			<!--2、我的应用-->
			<template>
				<view class="my-from" :key="typeIndex" v-for="(typeItem, typeIndex) in menuList">
					<view class="my-from-border">
						<view class="my-from-back"></view>
						<view class="my-from-title">{{typeItem.data.menuName}}</view>
					</view>
					<my-form-button :menuData="typeItem.children" @jump="jump" @click="menuClick"></my-form-button>
				</view>
			</template>
		</scroll-view>
	</view>
</template>

<script>
	import baseUrl from "@/common/constants/commonConstants.js"
	import uniNavBar from "@/components/uni-nav-bar.vue"
	import MyFormButton from './my-form-button.vue'
	import authMenuApi from "@/pages/api/authMenuApi.js"
	import CryptoJS from '@/uni_modules/crypto-js/crypto-js.js'
	import store from "@/store"
	import {
		encrypt,
		decrypt
	} from '@/common/js/des.js'
	export default {
		components: {
			MyFormButton,
			uniNavBar
		},
		data() {
			return {
				base: baseUrl,
				menuList: [],

			}
		},

		onLoad(res) {
			let that = this
			let userCode = ''
			let userName = '寰宇'
			if (res && res.account) {

				uni.setStorageSync('huazhengData', res);

				let option = {
					account: res.account
				}
				const result = this.decryptKunpeng(option, 'huanYuH51042', res.iv)
				console.log('解密', result)
				userCode = result.account.split('_')[0]
				uni.setStorage({
					key: "userInfo",
					data: {
						userCode: userCode,
						userName: userName
					},
					success() {
						that.doTokenLogin(userCode)
					}
				})
			} else {
				// uni.setStorageSync("userInfo", {
				// 	userCode: 'liuxinming',
				// 	userName: 'liuxinming'
				// })
				// let userinfo ={
				// 	userCode: 'liuxinming',
				// 	userName: 'liuxinming'
				// }
				let userinfo = uni.getStorageSync("userInfo")
				if (userinfo && userinfo.userCode) {
					userCode = userinfo.userCode
					userName = userinfo.userName
					that.doTokenLogin(userCode)
				} else {
					that.back()
				}

			}
		},
		methods: {
			doTokenLogin(userCode) {
				let that = this
				authMenuApi.tokenLogin({
					token: userCode,
					operatorUser: userCode
				}, (resData) => {
					console.log("doTokenLogin:", resData);
					uni.setStorageSync("userInfo", {
						userCode: resData.data.userCode,
						userName: resData.data.userName
					})

					uni.setStorageSync("token", encrypt(resData.data.token, 3))
					store.commit("saveToken", resData.data.token)

					that.getMenuList()
				})
			},
			back() {
				uni.removeStorageSync('token');
				let huazhengData = uni.getStorageSync('huazhengData')
				if (huazhengData) {
					location.href = "https://app.hydficqd.com:8082/#/pages/home/home?token" + huazhengData.token +
						'&iv=' + huazhengData.iv + '&account=' + huazhengData.account
				} else {
					uni.showToast({
						icon: 'error',
						title: '获取app数据为空'
					})
				}
			},
			decryptKunpeng(params, secretKey, ivBase64) {
				// 使用 SHA-256 将密钥转换为 32 字节（256 位）的密钥
				const keyBytes = CryptoJS.SHA256(secretKey);

				// Base64 解码 IV
				const iv = CryptoJS.enc.Base64.parse(decodeURIComponent(ivBase64));

				const result = Object.keys(params).reduce((pre, key) => {
					const value = decodeURIComponent(params[key])
					const parseValue = CryptoJS.AES.decrypt(value, keyBytes, {
						iv,
						mode: CryptoJS.mode.CBC,
						padding: CryptoJS.pad.Pkcs7,
					}).toString(CryptoJS.enc.Utf8);
					pre[key] = parseValue;
					return pre;
				}, {});
				return result;
			},

			testClick() {
				uni.navigateTo({
					url: "/pages/index/common-demo"
				})
			},
			menuClick() {

			},
			jump(item) {
				console.log("点击了---", item);

				uni.navigateTo({
					url: '../../../../../' + item.data.menuUrl + "?title=" + item.data.menuName + "&domainUrl=" +
						item.data.domainUrl + "&menuCode=" + item.data.menuCode,
					animationType: 'slide-in-bottom',
					success: function(res) {
						res.eventChannel.emit('jumpPage', {
							data: item
						})
						console.log('成功', res);
					},
					fail: function(res) {
						console.log(res);
					}
				})
			},
			getMenuList() {
				var that = this
				authMenuApi.getGrantAuthorizationNormalMenuTreeListByUser((result) => {
					// console.log(result.hasData, "======",result.list
					if (result.hasData) {
						// console.log(result.list, "======");
						this.menuList = result.list;

					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import url("./index.css");

	.container {
		width: 100%;
		height: 100%;
		position: fixed;
		overflow-y: scroll;
		overflow-x: hidden;
		background-color: #FFFFFF;
	}



	.my-from {
		width: 100%;
		padding-top: 20px;
	}

	.my-from-back {
		width: 2px;
		height: 15px;
		background-color: #1974CC;
	}

	.my-from-title {
		width: 100%;
		padding-left: 5px;
		font-family: sans-serif;
	}

	.my-from-border {
		width: 100%;
		height: 23px;
		padding-left: 20px;
		padding-bottom: 15px;
		display: flex;
		align-items: center;
		border-bottom: 0.5rpx solid #f5f5f5;
	}
</style>